package com.zjz.demo.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/10/12:06
 * @Description:
 * @Target:
 */
public class SpinLockTest {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();
        reentrantLock.lock();
        reentrantLock.unlock();



        // CAS 自旋锁
        SpinLockDemo lockDemo = new SpinLockDemo();

        new Thread(()->{
            lockDemo.myLock();

            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lockDemo.myUnLock();
            }

        },"T1").start();


        TimeUnit.SECONDS.sleep(2);


        new Thread(()->{
            lockDemo.myLock();

            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lockDemo.myUnLock();
            }

        },"T2").start();


    }
}
